package com.etl.controller;

import com.etl.entity.EtlUser;
import com.etl.model.rev.EtlUserRev;
import com.etl.model.rev.EtlUserSaveRev;
import com.etl.service.IEtlUserService;
import com.etl.util.MD5Util;
import com.etl.util.ResponseUtil;
import com.etl.util.ResultVO;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by Administrator on 2019/10/21.
 */
@RestController
@RequestMapping("/etlUser")
@Api(tags = {"用户管理"}, value = "用户管理相关接口")
@Slf4j
@Validated
public class EtlUserController {

    @Autowired
    private IEtlUserService IEtlUserService;

    @ApiOperation(value = "获取用户信息")
    @PutMapping(value = "/getEtlUser")
    public ResultVO<PageInfo<EtlUser>> getEtlUser(@Validated @RequestBody EtlUserRev etlUserRev, BindingResult bindingResult) {
        //校验参数是否为空
        if (bindingResult.hasErrors()) {
            List<ObjectError> errorList = bindingResult.getAllErrors();
            log.error("保存轨迹,错误参数" + errorList.get(0).getDefaultMessage());
            return ResponseUtil.error(0, errorList.get(0).getDefaultMessage());
        }

        PageInfo<EtlUser> pageInfo = IEtlUserService.getEtlUser(etlUserRev);
        return ResponseUtil.success(pageInfo);
    }

    @ApiOperation(value = "保存或者修改用户信息")
    @PutMapping(value = "/saveOrUpdateEtlUser")
    public ResultVO saveOrUpdateEtlUser(@Validated @RequestBody EtlUserSaveRev etlUserSaveRev, BindingResult bindingResult) {
        //校验参数是否为空
        if (bindingResult.hasErrors()) {
            List<ObjectError> errorList = bindingResult.getAllErrors();
            log.error("保存轨迹,错误参数" + errorList.get(0).getDefaultMessage());
            return ResponseUtil.error(0, errorList.get(0).getDefaultMessage());
        }

        try {
            etlUserSaveRev.setPassword(MD5Util.getMD5(etlUserSaveRev.getPassword()));
        } catch (MD5Util.MD5Exception e) {
            e.printStackTrace();
        }
        IEtlUserService.saveOrUpdateEtlUser(etlUserSaveRev);
        return ResponseUtil.success();
    }


}

